<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Es6符号</title>
    <script src="js/traceur.js"></script>
    <script src="js/bootstrap.js"></script>
    <script type="module">

        //symbol创建符号
        let s1 = window.Symbol("My Symbol");
        let s2 = window.Symbol("My Symbol");
        console.log(s1 === s2); // "false"

        //typeof操作符
        var s = Symbol();
        console.log(typeof s); //symbol

        //new操作符
        try
        {
            let s4 = new Symbol(); //"TypeError" 错误
        }
        catch(e)
        {
            console.log(e.message); //输出"Symbol is not a constructor"
        }

        //使用符号作为对象属性
        let obj = null;
        let s5 = null;
        (function(){
            let s6 = Symbol();
            s5 = s6;
            obj = {[s6]: "mySymbol"}
            console.log(obj[s6]); //mySymbol
            console.log(obj[s6] == obj[s5]); //true
        })();
        //由于s5跟s6的值相同，对于obj来说，是访问同一个属性
        console.log(obj[s5]); // mySymbol

        //获取对象的符号属性
        let obj1 = {a: 12};
        let s7 = Symbol("mySymbol");
        let s8 = Symbol("mySymbol");
        Object.defineProperty(obj1, s7, {
            enumerable: false
        });
        obj1[s8] = "";
        //Symbol(mySymbol),Symbol(mySymbol)
        console.log(Object.getOwnPropertySymbols(obj1));

        //在块中全局创建符号
        let obj2 = {};
        (function(){
            let s9 = Symbol("name");
            obj2[s9] = "Eden";
        })();
        //obj[s1] cannot be accessed here
        (function(){
            let s10 = Symbol.for("age");
            obj[s10] = 27;
        })();
        console.log(obj[Symbol.for("age")]); // "27"

    </script>
</head>
<body>

</body>
</html>